//
//  NotesAllCommentsViewController.m
//  TravelFirst
//
//  Created by lanou on 15/9/25.
//  Copyright (c) 2015年 吴娅君. All rights reserved.
//

#import "NotesAllCommentsViewController.h"
#import "NotesAllCommentsTableViewCell.h"
#import "NotesModel.h"

@interface NotesAllCommentsViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation NotesAllCommentsViewController


#pragma mark - 加载数据 -

- (void)reloadView
{
    NSString *urlStr = [kNotesAllPictureComments stringByReplacingOccurrencesOfString:@"参数" withString:self.log_id];
    [LORequestManger GET:urlStr success:^(id response) {
        
        NSDictionary *dic = (NSDictionary *)response;
        for (NSDictionary *commentsDic in dic[@"comments"]) {
            NotesModel *notesModel = [[NotesModel alloc] init];
            [notesModel shareJsonWithDictionary:commentsDic];
            [notesModel shareJsonWithDictionary:commentsDic[@"created_by"]];
            [notesModel shareJsonWithDictionary:commentsDic[@"post"][@"pictures"][0]];
            [self.dataArray addObject:notesModel];
        }
        
        if (self.dataArray && self.dataArray.count != 0) {
            [self.tableView reloadData];
        } else {
            [self setCoverView];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);
    }];
}


#pragma mark - viewDidLoad -

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.alpha = 1;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"评论";
    //self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    
    // 加载数据
    [self reloadView];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, self.view.height)];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[NotesAllCommentsTableViewCell class] forCellReuseIdentifier:@"NotesAllCommentsTableViewCell"];
    [self.view addSubview:self.tableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - tableView -

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NotesAllCommentsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NotesAllCommentsTableViewCell" forIndexPath:indexPath];
    NotesModel *model = self.dataArray[indexPath.row];
    [cell setValueWithModel:model];
    [cell setTarget:self avatorButtonAction:@selector(avatorButtonAction:) pictureButtonAction:@selector(pictureButtonAction:) tag:indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NotesModel *notesModel = self.dataArray[indexPath.row];
    return [NotesAllCommentsTableViewCell appendStringWithModel:notesModel] + 24;
}


#pragma mark - 遮挡视图 -

- (void)setCoverView
{
    // 输入框的高度44
    UIView *coverView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, kWidth, kHeight - 64 - 44)];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((kWidth - 40) / 2, coverView.height / 2 - 80, 40, 40)];
    imageView.image = [UIImage imageNamed:@"icon_comment_gray_32x32@2x"];
    [coverView addSubview:imageView];
    
    UILabel *hintLabel = [[UILabel alloc] initWithFrame:CGRectMake((kWidth - 200) / 2, (coverView.height - 30) / 2, 200, 30)];
    hintLabel.text = @"还没有评论，快来第一个发言~";
    hintLabel.textAlignment = NSTextAlignmentCenter;
    hintLabel.font = [UIFont systemFontOfSize:13];
    hintLabel.textColor = [UIColor grayColor];
    [coverView addSubview:hintLabel];
    
    [self.view addSubview:coverView];
}

#pragma mark - 点击方法 -

// 点击头像
- (void)avatorButtonAction:(UIButton *)button
{
    NSLog(@"%ld", button.tag);
    NSLog(@"个人信息");
}

// 点击评论后的图片
- (void)pictureButtonAction:(UIButton *)button
{
    NSLog(@"%ld", button.tag);
    NSLog(@"进入图片观看");
}


#pragma mark - 懒加载 -

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
